<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="DialogFormVisibleCom" :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="150px"
               style="width: 90%; margin:10px auto;">

        <el-row :gutter="20">

          <!-- 生成方案编号 -->
          <el-col :span="12">
            <el-form-item label="生成方案编号" prop="gs_s_code">
              <el-input v-model="formSubmitDataCom.gs_s_code" :disabled="true"/>
            </el-form-item>
          </el-col>

          <!-- 生成方案名称 -->
          <el-col :span="12">
            <el-form-item label="生成方案名称" prop="gs_s_name">
              <el-input v-model="formSubmitDataCom.gs_s_name"/>
            </el-form-item>
          </el-col>

          <!-- 生成方案生效日期 -->
          <el-col :span="12">
            <el-form-item label="生成方案生效日期" prop="gs_s_s_date">
              <el-date-picker v-model="formSubmitDataCom.gs_s_s_date" type="date" placeholder="Please pick a date"
                              value-format="yyyy-MM-dd"/>
            </el-form-item>
          </el-col>

          <!-- 生成方案失效日期 -->
          <el-col :span="12">
            <el-form-item label="生成方案失效日期" prop="gs_s_e_date">
              <el-date-picker v-model="formSubmitDataCom.gs_s_e_date" type="date" placeholder="Please pick a date"
                              value-format="yyyy-MM-dd"/>
            </el-form-item>
          </el-col>

          <!-- 生成曲线类型 -->
          <el-col :span="12">
            <el-form-item label="生成曲线类型" prop="gs_cv_type">
              <el-select v-model="formSubmitDataCom.gs_cv_type" class="filter-item" placeholder="Please select">
                <el-option v-for="item in gsCvTypeOptions" :key="item.code" :label="item.name"
                           :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- 生成曲线名称 -->
          <el-col :span="12">
            <el-form-item label="生成曲线名称" prop="gs_cv_name">
              <el-input v-model="formSubmitDataCom.gs_cv_name"/>
            </el-form-item>
          </el-col>


          <!-- 备注 -->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="formSubmitDataCom.remark" placeholder="Please select"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
import XEUtils from 'xe-utils'

export default {
  name: 'submitForm',
  components: {
    //PageHeaderLayout,
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    // tableColumn: {
    //     type: Array,
    //     default() {
    //         return []
    //     }
    // },
    formSubmitData: {
      type: Object,
      default: function () {
        return {
          id: "",
          gs_s_code: '',      //生成方案编号
          gs_s_name: '',     //生成方案名称
          gs_s_s_date: '',   //生成方案生效日期
          gs_s_e_date: '',  //生成方案失效日期
          gs_cv_type: '',    //生成曲线类型
          gs_cv_name: '',    //生成曲线名称
          status: '',        //状态 （1：有  0:无效）
          remark: '',        //备注
        }
      }
    },
    // 生成曲线类型select数据
    gsCvTypeOptions: {
      type: Array,
      default() {
        return []
      }
    }
  },
  // 定义属性
  data() {

    /* 生效日期验证 */
    var validateDateRangeSdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('生效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.e_date)) && (value > this.formSubmitDataCom.e_date)) {
        callback(new Error('生效日期必须小于失效日期!'));
      } else {
        callback();
      }
    };
    /* 失效日期验证 */
    var validateDateRangeEdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('失效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.s_date)) && (value < this.formSubmitDataCom.s_date)) {
        callback(new Error('失效日期必须大于生效日期!'));
      } else {
        callback();
      }
    };


    return {
      isFactorType: true,
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        gs_s_code: [{required: true, message: '生成方案编号必填', trigger: 'change'}],
        gs_s_name: [{required: true, message: '生成方案名称必填', trigger: 'change'}],
        gs_cv_type: [{required: true, message: '生成曲线类型必填', trigger: 'change'}],
        gs_cv_name: [{required: true, message: '生成曲线名称必填', trigger: 'change'}],

        gs_s_s_date: [{validator: validateDateRangeSdate, trigger: 'change'}],
        gs_s_e_date: [{validator: validateDateRangeEdate, trigger: 'change'}],
      },
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    },
    formSubmitData_factor_type() {
      return this.formSubmitData.factor_type
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    resetFormData() {

      let exclude = []
      let replaceObj = {}
      if (this.dialogStatus === 'create') {
        exclude = [
          'gs_s_code',
        ]
      }
      if (this.dialogStatus === 'update') {
        exclude = [
          'id',
          'gs_s_code',
        ]
      }
      this.$emit('resetFormDataEmit', {
        exclude,
        replaceObj
      })

    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$delete(this.formSubmitData, "_XID");
          const tempData = Object.assign({}, this.formSubmitData)
          this.$emit('handleFromUpdateDataEmit')
        }
      })
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>

</style>
